package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.model.sql.Result;
import com.cskaoyan.mall.model.bo.ChangeOrderBO;
import com.cskaoyan.mall.model.bo.PageOrderBO;
import com.cskaoyan.mall.model.vo.OrderVO;
import com.cskaoyan.mall.model.vo.PageOrdersVO;
import com.cskaoyan.mall.service.OrderService;
import com.cskaoyan.mall.service.implement.OrderServiceImpl;
import com.cskaoyan.mall.utils.HttpUtils;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {
    private OrderService orderService = new OrderServiceImpl();

    Gson gson =new Gson();

    /***********************************************    doPost   *******************************************************/

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if("ordersByPage".equals(action)){
            OrdersByPage(request,response);
        }else if ("changeOrder".equals(action)){
            changeOrder(request,response);
        }
    }

    /**
     * 修改订单
     * @param request
     * @param response
     */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        ChangeOrderBO changeOrderBO = gson.fromJson(requestBody, ChangeOrderBO.class);
        orderService.changeOrder(changeOrderBO);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * 根据条件分页查询显示订单
     * 1.获取请求参数
     * 2.业务处理
     * 3.响应
     * @param request
     * @param response
     */
    private void OrdersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        PageOrderBO orderBO = gson.fromJson(requestBody, PageOrderBO.class);
        PageOrdersVO orders = orderService.ordersByPage(orderBO);
        System.out.println(orders);
        response.getWriter().println(gson.toJson(Result.ok(orders)));
    }

    /***********************************************    doGet   *******************************************************/
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String action = requestURI.replace("/api/admin/order/", "");
        if("order".equals(action)){
            order(request,response);
        }else if ("deleteOrder".equals(action)){
            deleteOrder(request,response);
        }
    }

    /**
     * 删除 id= ？ 的订单
     * @param request
     * @param response
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        orderService.deleteOrder(id);
        response.getWriter().println(gson.toJson(Result.ok()));
    }

    /**
     * http://localhost:8084/api/admin/order/order?id=1
     * @param request
     * @param response
     */
    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        OrderVO orderVO = orderService.order(id);
        response.getWriter().println(gson.toJson(Result.ok(orderVO)));
    }
}
